<?php
namespace app\branch\model;
use think\Session;
use think\Model;
use think\Request;
/**
 * 分公司营地管理
 */
class Campcontrols extends model
{   
    use \app\common\traits\model\TraitModel;
    protected $table = 'camps';
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $type = [
            'cover' =>'array',
            'license'=>'array',
    ];
    /**
     * [getUserList 营地列表]
     * @param  [type] $querys [description]
     * @return [type]         [description]
     */
    public function getCampList($querys=[])
    {
        // if (array_key_exists('searchfield', $querys)&&empty($querys["searchfield"])) {
        //     $a =  ['name|count|tel|travel_money_base'=>['like', '%%'.$querys['keyword'].'%%']]; 
        //     $querys = array_merge($querys,$a);
        // }
        return $this->getInfoList($querys);
    }
    
    public function caravancontrols()
    {
        return $this->hasMany('Caravancontrols','services');
    }
    /**
     * [addCampInfo 添加营地]
     * @param [type] $post_camp_info [description]
     */
    public function addCampInfo($post_camp_info)
    {
       return $this->create($post_camp_info);
    }
    /**
     * [getUserIofo 营地详情信息]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getCampIofo($id)
    {
        return $this->where('id',$id)->find(); 
    }
    /**
     * 生成编号随机数
     * @return [type] [description]
     */
    public static function quickRandom($length = 16)
    {
        $pool = '0123456789';

        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }
    /**
     * 更新营地信息
     * @param  [type] $camp_id [description]
     * @param  [type] $post_camp_info [description]
     * @return [type]             [description]
     */
    public function updateCampInfo($camp_id, $post_camp_info)
    {
        //db函数中没有类型自动转换。模型函数有这类方法
        return $this->update($post_camp_info,['id'=> $camp_id]);
    }
    /**
     * [getSeekName description]
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function getSeekName()
    {

        $ziduan = ['name'=>'营地名称','count'=>'泊位数量','tel'=>'营地电话','travel_money_base'=>'基数'];
        return $ziduan;
    }
    /**
     * [getType 类型转换]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getType($id)
    {
         $types = db('camp_types')->column('name', 'id');
        if (array_key_exists($id, $types)) {
           return $types[$id];
        }
        return '无';
        //第二种方式
        // $types = model('CampTypes')->where('id',$id)->value('name');
        // return $types;
    }
    /**
     * [getGrade 类型转换]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getGrade($id)
    {
        $grades = db('camp_grades')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }

}
